package xlog

import (
	"gitee.com/igolang/pkg/xtime"
	"log/slog"
)

type Config struct {
	slog.HandlerOptions
	OutputPaths      []string
	Format           string
	ErrPaths         []string
	FileMaxSize      int
	BackUpFileMaxAge int
}

const (
	TextFormat = "text"
	JsonFormat = "json"
)

var replaceAttrFunc = func(groups []string, a slog.Attr) slog.Attr {
	if a.Key == slog.TimeKey {
		a.Value = slog.StringValue(a.Value.Time().Format(xtime.DateFormatTimestamp))
	}
	if a.Key == slog.SourceKey {
		a.Key = "caller"
	}
	return a
}

func (c *Config) check() {
	if c.Level == nil {
		c.Level = slog.LevelDebug
	}
	if c.ReplaceAttr == nil {
		c.ReplaceAttr = replaceAttrFunc
	}
	if len(c.OutputPaths) == 0 {
		c.OutputPaths = []string{"stdout"}
	}
	if c.Format == "" {
		c.Format = JsonFormat
	}
}

func DefaultConfig() *Config {
	return &Config{
		HandlerOptions: slog.HandlerOptions{
			Level:       slog.LevelDebug,
			AddSource:   true,
			ReplaceAttr: replaceAttrFunc,
		},
		OutputPaths: []string{"stdout"},
		Format:      JsonFormat,
	}
}
